begintownscript;
variables
short caster,choice,i,j,highest,strength,tick;
string caststrng,spell;
body;

beginstate init_state;
set_crime_tolerance(3);
tick = get_current_tick();
	if((get_flag(0,8) == 0) && (get_flag(250,2) == 0))
		activate_hidden_group(1);		
	if(get_flag(0,12) == 0) {
		toggle_quest(2,1);
		set_flag(0,12,1); }
	if((get_flag(0,13) == 0) || (get_flag(1,3) == 1))
		activate_hidden_group(2);

set_name(6,"Mack");
set_char_dialogue_pic(6,1958,0);
set_name(7,"Sara");
set_char_dialogue_pic(7,529,0);
set_name(8,"Arum");
set_char_dialogue_pic(8,528,0);
set_name(9,"Cecil");
set_char_dialogue_pic(9,530,0);
set_name(10,"Wallace");
set_char_dialogue_pic(10,1962,0);
set_name(11,"Kyra");
set_char_dialogue_pic(11,1947,0);
set_name(19,"Hmmhrrrr");
set_char_dialogue_pic(19,1924,0);
set_name(20,"Alonzo");
set_char_dialogue_pic(20,527,0);
set_name(21,"Tuchin");
set_char_dialogue_pic(21,1961,0);
set_name(23,"Glup");
set_name(24,"Mayor White");
set_char_dialogue_pic(24,1955,0);
set_name(25,"Erma");
set_char_dialogue_pic(25,1951,0);
break;

beginstate exit_state;
break;

beginstate start_state;
if(get_ran(1,0,100) < 7)
	text_bubble_on_char(8,"*Carve*");
if(get_ran(1,0,100) < 7)
	text_bubble_on_char(6,"*Clang* *Clang*");
if(get_ran(1,0,100) < 7)
	text_bubble_on_char(6,"Pretty ragged lookin' arrows.");
if(get_ran(1,0,100) < 7)
	text_bubble_on_char(8,"Hey Mack, how's that pot coming?");
if(get_ran(1,0,100) < 7)
	text_bubble_on_char(6,"Cut your hair!");
if(get_ran(1,0,100) < 7)
	text_bubble_on_char(8,"All hail the King of Tinkers!");
if(get_ran(1,0,100) < 6)
	text_bubble_on_char(7,"Sigh");
if(get_ran(1,0,100) < 6)
	text_bubble_on_char(9,"What can I get you?");
if(get_ran(1,0,100) < 6)
	text_bubble_on_char(9,"So bored...");
if(get_ran(1,0,100) < 6)
	text_bubble_on_char(11,"Where's my lazy son?");
if(get_ran(1,0,100) < 6)
	text_bubble_on_char(11,"Too busy to chat.");
if(get_ran(1,0,100) < 7)
	text_bubble_on_char(12,"I'm TRYING to study.");
if(get_ran(1,0,100) < 7)
	text_bubble_on_char(19,"........");
if(get_ran(1,0,100) < 6)
	text_bubble_on_char(20,"How 'bout we look here?");
if(get_ran(1,0,100) < 6)
	text_bubble_on_char(20,"Arrows bounce right off them.");
if(get_flag(250,7) == 0)
{	if(get_ran(1,0,100) < 7)
		text_bubble_on_char(21,"That might work...");
	if(get_ran(1,0,100) < 7)
		text_bubble_on_char(21,"Have to hold the golems off...."); }
else
{	if(get_ran(1,0,100) < 7)
		text_bubble_on_char(21,"Thank goodness.");
	if(get_ran(1,0,100) < 7)
		text_bubble_on_char(21,"The Academy is safe!"); }
if(get_ran(1,0,100) < 8)
	text_bubble_on_char(22,"Wanna see me jump?");
if(get_ran(1,0,100) < 8)
	text_bubble_on_char(22,"Eeek a frog!");
if(get_ran(1,0,100) < 8)
	text_bubble_on_char(22,"I once saw a tiger!");
if(get_flag(250,7) == 0)
{	if(get_ran(1,0,100) < 7)
		text_bubble_on_char(24,"No time to sleep!");
	if(get_ran(1,0,100) < 7)
		text_bubble_on_char(24,"Finally you came!"); }
else
{	if(get_ran(1,0,100) < 7)
		text_bubble_on_char(24,"Thank you so much!");
	if(get_ran(1,0,100) < 7)
		text_bubble_on_char(24,"You're heroes!"); }


if(get_flag(0,25) == 0)
{	if((get_current_tick() - tick) > 3)
	{
	highest = 0;
		j = 0;
		while (j < 4)
		{	strength = (get_stat(j,11) + get_stat(j,12));
			if (strength > highest)
			{	highest = strength;
				caster = j; }
			j = j + 1;
			}
	set_flag(0,25,1);
	if(highest > 0)
	{	if(party_size() > 1)
		{	clear_buffer();
			append_char_name(caster);
			append_string(" turns to you, _I've been feeling strange for a while now. I believe it began when we were approaching Keilan. I've never felt anything like it: when I try to cast magic, it feels as if my head is fuzzy._");
			get_buffer_text(caststrng);
			message_dialog(caststrng,"You ask what could be causing this. _I don't know, it feels like a mild dumbfounding. It is the oddest thing I've ever felt._ You suggest it might be something in the water. _Your guess is as good as mine. I think I should sit down._");
}
		else	message_dialog("You've been feeling strange for a while now. You've been feeling it since you began approaching Keilan, but now that you are actually in the city it has intensified. It feels vaguely like a minor dumbfounding; it is very unsettling.","You feel lightheaded and muddled when you try to cast magic. You can hardly wait to get away from whatever is causing this.");
}
}
}

break;

beginstate 10;
	move_to_new_town(1,17,7);
break;

beginstate 11;
	move_to_new_town(1,38,38);
break;

beginstate 12;
	message_dialog("A note has been tacked onto this door. It reads, _Left Keilan for Imperius._ Looks as though people are abandoning Keilan for fear of the golems.","");
break;

beginstate 13;
	message_dialog("The trap door is stuck, but after enough hauling on the handle, it squeals open on long-unused 	hinges.","");
	move_to_new_town(2,26,38);
	
break;

beginstate 14;
break;
beginstate 15;
	reset_dialog();
	reset_dialog();	
	add_dialog_str(0,"This tombstone is fairly worn and old. Moss has begun to grow on its surface; however, you can still read it.",0);
	add_dialog_str(1,"_Dilecia, maiden most fair,",70);
	add_dialog_str(2,"Daughter most dutiful,",70);
	add_dialog_str(3,"Companion most beloved,",70);
	add_dialog_str(4,"Voice to shame the nightingale,",70);
	add_dialog_str(5,"May your eternal slumber",70);
	add_dialog_str(6,"Be the most peaceful you have ever known_",70);
	add_dialog_choice(0,"OK");
	choice = run_dialog(1);
	if (choice == 1)
	end();
break;
beginstate 16;
	if(get_flag(0,1) == 0) { 
		message_dialog("You find a trap door leading to the tavern's wine cellar. The air is cool and dank, hidden from the heat of the sun. The casks of wine are in ordered rows, plainly labeled with their vintage and age.","");
		set_flag(0,1,1); }
		move_to_new_town(1,8,38);
break;
beginstate 17;
	message_dialog("The trap door glides open on well-greased hinges. The steps down are worn--many people must pass this way.","");
	move_to_new_town(1,9,28);
break;

beginstate 18;
	if(get_flag(0,16) == 0) {
		message_dialog("The Academy of Keilan is an above-average-sized building to adventurers who have travelled all across Pralgad, but it is colossal compared to the other buildings in Keilan. It is three stories tall and dominates the entire city.","");
		set_flag(0,16,1);
}
break;

beginstate 19;
	if(get_flag(0,17) == 0) {
		message_dialog("You step into the Academy and are stunned. You can see your reflections in the polished tile floor, and the hall's ceiling extends all the way to the second floor, a very impressive sight. Several students wander about, studying.","Several waterfalls empty into a pool on the northern wall. The path down to the pool is flanked by stone columns, and the walls are adorned with magnificent tapestries. You did not expect fo find workmanship like this in a town as small as Keilan.");
		set_flag(0,17,1);
}
break;

beginstate 20;
	message_dialog("This book is a great reminder of why you chose to become an adventurer. It is an account of any numerical stat imaginable regarding Keilan's situation this year, full of sums, reports, budgets, taxes, and inventories.","You leaf through several pages before closing the cover. Adventurers don't fear many things, but reading ledgers is certainly on the list.");
break;

beginstate 21;
	if(get_flag(0,18) == 0) {
		message_dialog("You walk into Keilan's inn. The sign reads _The Smarmy Swashbuckler._ Just below is painted a picture of a dangerous-looking warrior flirting with a tavern girl.","When you enter the common room, you find an array of characters. This would be a good place to gather information for your quest.");
		set_flag(0,18,1); }
break;

beginstate 22;
reset_dialog();
add_dialog_str(0,"A large tome sits on the pedestal before you. Your fingers itch to open it up and see what powerful incantations have been written within.",0);
add_dialog_str(1,"But your experience as an adventurer causes you to hesitate, wizards are notorious for hoarding their knowledge, very rarely is a powerful spell book left unguarded. Proceeding could have painful consequences. What do you do?",0);
add_dialog_choice(0,"Open it.");
add_dialog_choice(1,"Leave.");
choice = run_dialog(1);
if(choice == 1)
{	if(get_flag(0,21) == 0)
	{	message_dialog("Sparks jump up at you as you try to open the books cover. You persist and receive a powerful jolt up your arm. _Please don't touch that,_ Tuchin admonishes.","");
		i = 0;
		while(i <= 5)
		{	if(char_ok(i) == 1)
				damage_char(i,(get_ran(10,20,30)),3);
			i = i + 1; }
	}
	if(get_flag(0,21) > 0)
	{	
	if(get_skill_total(42) >= 30)
		if(get_flag(0,22) == 0)
		{	message_dialog("You turn through the spell book and learn a variety of powerful incantations. Anyone in your party with a high enough level has improved his ability in Arcane Blow and Arcane Summon.","");
	i = 0;	
	while (i < 4)
		{	if(get_spell_level(i,0,19) > 3)
			{	clear_buffer();
				append_char_name(i);
				append_string(" already knows Arcane Blow at this level");
				get_buffer_text(spell);
				print_str(spell); }
			if(get_spell_level(i,0,19) <= 3)
				change_spell_level(i,0,19,1);
			i = i + 1; }
	i = 0;
		while (i < 4)
		{	if(get_spell_level(i,0,17) > 3)
			{	clear_buffer();
				append_char_name(i);
				append_string(" already knows Arcane Summon at this level");
				get_buffer_text(spell);
				print_str(spell); }
			if(get_spell_level(i,0,17) <= 3)
				change_spell_level(i,0,17,1);
			i = i + 1; }
			set_flag(0,22,1);
			end(); }
		if(get_flag(0,22) > 0)
			message_dialog("You look over Tuchin's tome again, but learn nothing new.","");
	if(get_skill_total(42) < 30)
		message_dialog("You try to puzzle through Tuchin's notes on how to perform various rituals and spells, but quickly grow confused. You need higher rune reading skill to understand this.","");
}
}
if(choice == 2)
	end();
break;
		
beginstate 23;
	if(get_flag(0,23) == 0)
	{	message_dialog("The plaque on the door reads _Residence of Mayor White._ Mayor white certainly lives lavishly.","");
		set_flag(0,23,1); }
break;

